#include <stdio.h>
#include <string.h>

#define file "test.txt" 

void func1()
{

    char buffer[64];
    fscanf(stdin,"%s",buffer);
    printf("%s\n",buffer);
    fputs(buffer,stdout);
    printf("\n");

    const char* str = "4321";
    fwrite(str,1,strlen(str),stdout);//从字符串中写入
    printf("\n");
    fprintf(stdout,"%s",str);
    printf("\n");
}

//从文件中读取内容
int func2()
{
    FILE* fp = fopen(file,"r");
    if(fp==NULL)//未能打开文件
    {
        perror("未能打开文件");
        return -1;
    }
    char buffer[64];
    while(1)
    {
        char* tmp = fgets(buffer,sizeof(buffer),fp);//从文件中读取内容
        if(tmp==NULL)//读取成功返回指向该串的指针，读取失败返回空指针
            break;
        printf("%s\n",buffer);
        //printf("%s\n",tmp);
    }


    fclose(fp);
    return 1;

}
//向文件中写入
int func3()
{
    FILE* fp = fopen(file,"a");
    if(fp==NULL)//未能打开文件
    {
        perror("未能打开文件");
        return -1;
    }

    const char* str = "---------";
    int cnt = 9;
    while(cnt--)
    {
        fwrite(str,1,strlen(str),fp);
    }
    fclose(fp);

    return 1;
}

#define ONE 1
#define TWO (1<<1)
#define TREE (1<<2)
#define FOUR (1<<3)

void print(int ret)
{
    if(ret&ONE)
        printf("1 ");
    if(ret&TWO)
        printf("2 ");
    if(ret&TREE)
        printf("3 ");
    if(ret&FOUR)
        printf("4 ");
    printf("\n");
}

void func4()
{
    print(ONE);
    printf("-------------------\n");
    print(TWO);
    printf("-------------------\n");
    print(ONE|TREE);
    printf("-------------------\n");
    print(TWO|FOUR);
    printf("-------------------\n");

}
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int func5()
{

    //umask(0);
    //int fd = open("log.txt",O_WRONLY|O_CREAT|O_APPEND,0777);//以写方式打开，文件不存在就创建设置初始权限为0777，并且追加
    //if(fd==-1)
    //{
    //    perror("打开文件失败");
    //    return -1;
    //}
    //const char* str = "abcdefgh";
    //write(fd,str,strlen(str));

    //stdin 0
    //stdout 1
    //stderr 2
   

    //把文件中的内容读取出来
    char buffer[1024];
    int fd = open("log.txt",O_RDONLY);
    int ret = read(fd,buffer,sizeof(buffer));
    if(ret>0)//ret表示读取字节个数
    {
        buffer[ret] = 0;
        printf("%s",buffer);
    }
    close(fd);


    
    
    
    //int fd1 = open("log.txt",O_WRONLY|O_CREAT|O_APPEND,0777);
    //int fd2 = open("log.txt",O_WRONLY|O_CREAT|O_APPEND,0777);
    //int fd3 = open("log.txt",O_WRONLY|O_CREAT|O_APPEND,0777);
    //int fd4 = open("log.txt",O_WRONLY|O_CREAT|O_APPEND,0777);

    //printf("fd1:%d\n",fd1);
    //printf("fd2:%d\n",fd2);
    //printf("fd3:%d\n",fd3);
    //printf("fd4:%d\n",fd4);

    //close(fd1);
    //close(fd2);
    //close(fd3);
    //close(fd4);
    
    return 1;

}

void func6()
{
    const char* str = "aaaaaaaaaaaaa";    
    int fd = open("log.txt",O_WRONLY|O_CREAT|O_APPEND,0777);
    dup2(fd,1);
    printf("%s",str);

    int fd1 = open("log.txt",O_RDONLY);
    dup2(fd1,0);
    char buffer[1024];
    while(1)
    {
        char* tmp = fgets(buffer,sizeof(buffer),stdin); //stdin-->fd1   
        if(tmp==NULL)
            break;
        printf("%s",buffer);
        
    }
}


int main()
{
    func6();
    return 0;
}
